package com.qf.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.qf.ssm.dto.R;
import com.qf.ssm.pojo.Devices;
import com.qf.ssm.service.DevicesService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/devices")
public class DevicesController  {

    private DevicesService devicesService;

    @Autowired
    public void setDevicesService(DevicesService devicesService) {
        this.devicesService = devicesService;
    }

    @PostMapping("/device")
    public R addDevices(@RequestBody Devices devices)throws Exception{
        devicesService.addDevices(devices);
        return R.setOK(null);
    }
    @PutMapping("/device")
    public R updateDevices(@RequestBody Devices devices){
        devicesService.updateDevices(devices);
        return R.setOK(devices);
    }
    @DeleteMapping("/device/{id}")
    public R deleteDevicesByIds(Long [] ids){
        devicesService.deleteDevicesByIds(ids);
        return R.setOK();
    }

    @GetMapping("/devices")
    public R getAllDevices(@RequestParam(defaultValue = "1",required = false) int page, @RequestParam(required = false,defaultValue = "10") int limit,@RequestParam(required = false)  String address,   @RequestParam(required = false,defaultValue = "-100") Integer status,   @RequestParam(required = false,defaultValue = "-100")  int direction,   @RequestParam(required = false,defaultValue = "-100") int areaid,  @RequestParam(required = false,defaultValue = "-100") int categoryid) {
        PageInfo<Devices> pageInfo = devicesService.getAllDevices(page, limit, address, status, direction,areaid,categoryid);
        return R.setOK(pageInfo);
    }

    /**
     *
     * @param uuid 设备编号,也是我们 websocket 的唯一标识
     * @param command //命令
     */
    @PostMapping("/command/{uuid}/{command}")
    public void sendCommand(@PathVariable  String uuid, @PathVariable  String command) {
        devicesService.sendCommand(uuid, command);
    }
}
